﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace PresentationLayer.Presenters
{
    using PresentationLayer.Views.ViewContracts;
    using PresentationLayer.Views;
    using PresentationLayer.Properties;
    using WinFormsMvp;
    using EngineLayer.Services;
    using System.Windows.Forms;
    using EngineLayer.Properties;

    public class LoginPresenter : Presenter<ILoginView>
    {  
        private string _UserName;
        private string _Password;

        public LoginPresenter(ILoginView view)
            : base(view)
        {
          //  Program.User = new EngineLayer.LoginModel();
            View.TxtPasswordLostFocus += View_TxtPasswordLostFocus;
            View.TxtLoginIDLostFocus += View_TxtLoginIDLostFocus;
            View.ProcessLogin += View_ProcessLogin;
            View.CloseFormClicked += View_CloseFormClicked;
            View.Load += View_Load;
        }

        private void View_TxtLoginIDLostFocus(object sender, EventArgs e)
        {
            _UserName = ((TextBox)sender).Text;
        }

        private void View_TxtPasswordLostFocus(object sender, EventArgs e)
        {
            _Password = ((TextBox)sender).Text;
        }

        private void View_CloseFormClicked(object sender, EventArgs e)
        {
            View.CloseForm();
        }

        private void View_ProcessLogin(object sender, EventArgs e)
        {
            try
            {
            //    Program.User.Validate(_UserName, _Password);
            }
            catch(Exception ex)
            {
                if(ex.Message == EngineResources.Invalid)
                    MessageBox.Show(Resources.VerifiqueUsuarioOContrasena, Resources.MaximosIntentosPermitidos, MessageBoxButtons.OK, MessageBoxIcon.Stop);
               
                this.View_CloseFormClicked(null, EventArgs.Empty);
            }
        }

        private void View_Load(object sender, EventArgs e)
        {
              View.Model = new EngineLayer.LoginModel();
        }

    }
}
